package com.zx.cloud;

import java.io.File;
import java.sql.DatabaseMetaData;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.bee.tl.core.GroupTemplate;
import org.bee.tl.core.Template;

import com.zx.cloud.def.TableMetadata;
import com.zx.cloud.trans.TableTrans;
import com.zx.cloud.utils.DBUtils;
import com.zx.cloud.utils.DateTimeUtil;

public class Main {

	public static final String ROOT_FILE_PATH = Main.class.getResource("/").getPath().replace("%20", " ");
	public static final String PACKAGE_PATH = ROOT_FILE_PATH+ "com/zx/cloud/tpl";
	public static final String GEN_PACKAGE = "com.zx.cloud";
	public static final GroupTemplate group = new GroupTemplate(new File(PACKAGE_PATH));

	public static final String GEN_PATH_DAO = "/trade-provider/src/main/java/com/zx/cloud/persistence/";
	public static final String GEN_PATH_MAPPER = "/trade-provider/src/main/resources/mapper/";
	public static final String GEN_PATH_ENTITY = "/trade-common-model/src/main/java/com/zx/cloud/domain/";
	public static final String GEN_PATH_SERVICE = "/trade-provider/src/main/java/com/zx/cloud/service/";
	public static final String GEN_PATH_CONTROLLER = "/trade-provider/src/main/java/com/zx/cloud/controller/";


	public static void main(String[] args) throws Exception {
		String relativelyPath = new File(System.getProperty("user.dir")).getParent().replace("\\", "/");
		
		System.out.println("----relativelyPath---"+relativelyPath);
		
		group.setCharset("UTF-8");
		DBUtils.initProperties();
		DatabaseMetaData dbmd = DBUtils.getDatabaseMetaData();
		String dbType = dbmd.getDatabaseProductName();
		DBUtils.loadMetadata(dbmd);
		if (DBUtils.genertorTableNames.size() > 0) {
			for (String tableName : TableMetadata.getAllTables().keySet()) {
				for (String key : DBUtils.genertorTableNames) {
					if (key.equals(tableName)) {
						generate(tableName, dbType, relativelyPath);
					}
				}
			}
		} else {
			for (String tableName : TableMetadata.getAllTables().keySet()) {
				generate(tableName, dbType, relativelyPath);
			}
		}
	}

	private static void generate(String tableName, String dbType,String relativelyPath) throws Exception {
		String daoPath = relativelyPath + GEN_PATH_DAO;
		String mappingPath = relativelyPath + GEN_PATH_MAPPER;
		String entityPath = relativelyPath + GEN_PATH_ENTITY;
		String servicePath = relativelyPath + GEN_PATH_SERVICE;
		String controllerPath = relativelyPath + GEN_PATH_CONTROLLER;
		
		 String cNmaeString=DBUtils.genertorTableChinseNames.get(tableName);
		
		if (DBUtils.genertor_mapper) {
			generateXml(mappingPath,cNmaeString, tableName, dbType);
		}
		if (DBUtils.genertor_controller) {
			generateWeb(controllerPath,cNmaeString, tableName, dbType);
		}
		if (DBUtils.genertor_dao) {
			generateDao(daoPath,cNmaeString, tableName, dbType);
		}
		if (DBUtils.genertor_model) {
			generateEntity(entityPath,cNmaeString, tableName, dbType);
		}
		if (DBUtils.genertor_model) {
			generateEntity(entityPath,cNmaeString, tableName, dbType);
		}
		if (DBUtils.genertor_service) {
			generateService(servicePath, cNmaeString,tableName, dbType);
		}
	}

	private static void generateXml(String basePath,String cNmaeString, String tableName,
			String dbType) throws Exception {
		System.out.println("basePath "+basePath);
		Template template = group.getFileTemplate(dbType + "-dao-xml.txt");
		if (template == null)
			throw new RuntimeException(String.format("未支持的数据库类型【%s】", dbType));
		TableTrans trans = TableTrans.find(tableName);
		template.set("date", DateTimeUtil.formatDatetoString(new Date()));
		template.set("time", DateTimeUtil.formatTimetoString(new Date()));
		template.set("startTag_", "${");
		template.set("endTag_", "}");
		template.set("package", GEN_PACKAGE);
		template.set("table", trans);
		template.set("chinaName", cNmaeString);
		template.set("model", null);
		System.out.println("----------basePath-------"+basePath);
		FileUtils.write(new File(basePath, trans.getUpperStartClassName()
				+ "Mapper.xml"), template.getTextAsString(), "UTF-8");
	}

	private static void generateDao(String basePath, String cNmaeString,String tableName,
			String dbType) throws Exception {
		Template template = group.getFileTemplate(dbType + "-dao-java.txt");
		if (template == null)
			throw new RuntimeException(String.format("未支持的数据库类型【%s】", dbType));
		TableTrans trans = TableTrans.find(tableName);
		template.set("date", DateTimeUtil.formatDatetoString(new Date()));
		template.set("time", DateTimeUtil.formatTimetoString(new Date()));
		template.set("startTag_", "${");
		template.set("endTag_", "}");
		template.set("package", GEN_PACKAGE);
		template.set("table", trans);
		template.set("chinaName", cNmaeString);
		template.set("model", null);
		FileUtils.write(new File(basePath, trans.getUpperStartClassName()
				+ "Mapper.java"), template.getTextAsString(), "UTF-8");
	}

	private static void generateWeb(String basePath,String cNmaeString, String tableName,
			String dbType) throws Exception {
		Template template = group.getFileTemplate(dbType + "-web-java.txt");
		if (template == null)
			throw new RuntimeException(String.format("未支持的数据库类型【%s】", dbType));
		TableTrans trans = TableTrans.find(tableName);
		template.set("date", DateTimeUtil.formatDatetoString(new Date()));
		template.set("time", DateTimeUtil.formatTimetoString(new Date()));
		template.set("startTag_", "${");
		template.set("endTag_", "}");
		template.set("package", GEN_PACKAGE);
		template.set("table", trans);
		template.set("chinaName", cNmaeString);
		template.set("model", null);
		FileUtils.write(new File(basePath, trans.getUpperStartClassName()
				+ "Controller.java"), template.getTextAsString(), "UTF-8");
	}

	
	private static void generateService(String basePath, String cNmaeString,String tableName,
			String dbType) throws Exception {
		Template template = group.getFileTemplate(dbType + "-service-java.txt");
		if (template == null)
			throw new RuntimeException(String.format("未支持的数据库类型【%s】", dbType));
		TableTrans trans = TableTrans.find(tableName);
		template.set("date", DateTimeUtil.formatDatetoString(new Date()));
		template.set("time", DateTimeUtil.formatTimetoString(new Date()));
		template.set("startTag_", "${");
		template.set("endTag_", "}");
		template.set("package", GEN_PACKAGE);
		template.set("table", trans);
		template.set("chinaName", cNmaeString);
		template.set("model", null);
		FileUtils.write(new File(basePath, trans.getUpperStartClassName()
				+ "Service.java"), template.getTextAsString(), "UTF-8");
	}

	private static void generateEntity(String basePath,String cNmaeString, String tableName,
			String dbType) throws Exception {
		Template template = group.getFileTemplate(dbType + "-domain.txt");
		if (template == null)
			throw new RuntimeException(String.format("未支持的数据库类型【%s】", dbType));
		TableTrans trans = TableTrans.find(tableName);
		template.set("date", DateTimeUtil.formatDatetoString(new Date()));
		template.set("time", DateTimeUtil.formatTimetoString(new Date()));
		template.set("startTag_", "${");
		template.set("endTag_", "}");
		template.set("package", GEN_PACKAGE);
		template.set("entityName", tableName);
		template.set("table", trans);
		template.set("chinaName", cNmaeString);
		template.set("model", null);
		System.out.println("basePath"+basePath);
		FileUtils.write(new File(basePath, trans.getUpperStartClassName()
				+ ".java"), template.getTextAsString());
	}
}
